package com.zlm.app.emp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zlm.app.emp.service.UserEmpService;
import com.zlm.common.util.CheckParamsUtils;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.EmpPunchParamsDto;
import com.zlm.common.entity.UserTokenDO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 考勤页面接口
 * @Author: duyufu
 * @Date: 2019/5/24 15:26
 */
@RestController
@RequestMapping(value = "/punch")
public class PunchController extends CheckController{
    @Autowired
    private UserEmpService userEmpService;

    /**
     * 获取指定日期考勤
     * @param token
     * @param punchDate
     * @param workTimeId
     * @return
     */
    @GetMapping("/queryEmpPunch")
    public JsonResultDto queryEmpPunch(String token,String punchDate,Integer workTimeId) {
        try {
            if (StringUtils.isBlank(punchDate)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.queryEmpPunch(userToken.getUserId(),punchDate,workTimeId);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取一天的考勤
     * @param token
     * @param punchDate
     * @return
     */
    @GetMapping("/queryEmpPunchDay")
    public JsonResultDto queryEmpPunchDay(String token,String punchDate) {
        try {
            if (StringUtils.isBlank(punchDate)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.queryEmpPunchDay(userToken.getUserId(),punchDate);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (RuntimeException e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 考勤打卡
     * @param empPunchParamsDto
     * @return
     */
    @PostMapping("/empPunch")
    public JsonResultDto empPunch(EmpPunchParamsDto empPunchParamsDto) {
        try {
            //检查必要参数
            if (!CheckParamsUtils.checkPunch(empPunchParamsDto)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            //判断token失效
            UserTokenDO userToken = checkToken(empPunchParamsDto.getToken());
            empPunchParamsDto.setEmpId(userToken.getUserId());
            JsonResultDto result = userEmpService.empPunch(empPunchParamsDto);
            checkJsonResultDto(result);
            return success();
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 获取班制列表
     * @param token
     * @return
     */
    @GetMapping("/queryOrgWorkTime")
    public JsonResultDto queryOrgWorkTime(String token) {
        try {
            //判断token失效
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.queryOrgWorkTime(userToken.getUserId());
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 考勤统计
     * @param token
     * @param date
     * @return
     */
    @GetMapping("/countPunch")
    public JsonResultDto countPunch(String token, String date) {
        try {
            if(StringUtils.isBlank(date)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.countPunch(userToken.getUserId(),date);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }

    /**
     * 考勤详情
     * @param token
     * @param date
     * @return
     */
    @GetMapping("/punchDetail")
    public JsonResultDto punchDetail(String token, String date) {
        try {
            if(StringUtils.isBlank(date)) {
                throw new RuntimeException(CodeConstants.CODE_PARAMS);
            }
            UserTokenDO userToken = checkToken(token);
            JsonResultDto result = userEmpService.punchDetail(userToken.getUserId(),date);
            checkJsonResultDto(result);
            return success(result.getData());
        } catch (Exception e) {
            e.printStackTrace();
            return fail(e.getMessage());
        }
    }


}
